#include "Widget.h"
#include "TexturePool.h"

Widget::Widget(void)
{
	texture = NULL;
	area.x = 0;
	area.y = 0;

	name = "Unknown";
	setActive(true);
	setVisible(true);
}


Widget::~Widget(void)
{
	if(texture != NULL)
	{
		SDL_DestroyTexture(texture);
	}
}


SDL_Rect Widget::getArea()
{
	return area;
}

void Widget::setArea(SDL_Rect area)
{
	this->area = area;
}

int Widget::getX()
{
	return area.x;
}

int Widget::getY()
{
	return area.y;
}

void Widget::setPosition(int x,int y)
{
	area.x = x;
	area.y = y;
}

void Widget::setWidth(int width)
{
	area.w = width;
}

void Widget::setHeight(int height)
{
	area.h = height;
}

int Widget::getWidth()
{
	return area.w;
}
	
int Widget::getHeight()
{
	return area.h;
}

void Widget::setName(std::string name)
{
	this->name = name;
}
std::string Widget::getName()
{
	return name;
}

bool Widget::isPointInside(int x,int y)
{
	if(x >= area.x && x <= area.x + area.w && y >= area.y && y <= area.y + area.w)
	{
		return true;
	}
	else return false;
}

void Widget::loadSkinFromFile(const char * fileName)
{
	texture = TexturePool::getPtr()->getTexture(fileName,area.w,area.h);
}

bool Widget::isActive()
{
	return active;
}

void Widget::setActive(bool active)
{
	this->active = active;
}

void Widget::setVisible(bool visible)
{
	this->visible = visible;
}

bool Widget::isVisible()
{
	return visible;
}

void Widget::addAddOn(IDrawable * obj)
{
	addOns.push_back(obj);
}
void Widget::deleteAddOn(IDrawable * obj)
{
	for(addOnIt = addOns.begin();addOnIt != addOns.end();++addOnIt)
	{
		if((*addOnIt) == obj)
		{
			delete (*addOnIt);
			addOns.erase(addOnIt);
			break;
		}
	}
}
void Widget::removeAddOn(IDrawable * obj)
{
	for(addOnIt = addOns.begin();addOnIt != addOns.end();++addOnIt)
	{
		if((*addOnIt) == obj)
		{
			addOns.erase(addOnIt);
			break;
		}
	}
}

void Widget::drawAddOns(SDL_Renderer * renderer)
{
	for(addOnIt = addOns.begin();addOnIt != addOns.end();++addOnIt)
	{
		(*addOnIt)->draw(renderer);
	}
}